package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

@Service
@Transactional //事务注解
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        if(ids!=null){
            for(Long roleId:ids){
                employeeMapper.insertRelation(employee.getId(),roleId);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRelation(employee.getId());
        if(ids!=null){
            for(Long roleId:ids){
                employeeMapper.insertRelation(employee.getId(),roleId);
            }
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employee= employeeMapper.selectForList(qo);
        return new PageInfo<>(employee);
    }

    @Override
    public Employee empLogin(String username, String password) {
        if(StringUtil.isEmpty(username)){
            throw new BusinessException("账号不能为空,傻逼");
        }
        if(StringUtil.isEmpty(password)){
            throw new BusinessException("密码不能为空,傻逼");
        }
       Employee emp= employeeMapper.selectByUsername(username);
        if(emp==null||!emp.getPassword().equals(password)){
            throw new BusinessException("全错,即将启动自爆程序");
        }
        return emp;
    }

    @Override
    public void exportXls(ServletOutputStream outputStream) throws IOException {
        //定义工作表
        Workbook wb=new HSSFWorkbook();
        //定义工作页
        Sheet sheet=wb.createSheet("员工表单");
        //定义行
        Row row = sheet.createRow(0);
        //定义表头
        row.createCell(0).setCellValue("员工编号");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("员工邮箱");

        List<Employee> employees = employeeMapper.selectAll();
        for(int i=0;i<employees.size();i++){
            Employee emp = employees.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(emp.getId());
            row.createCell(1).setCellValue(emp.getName());
            row.createCell(2).setCellValue(emp.getEmail());
        }

        //输出到浏览器
        wb.write(outputStream);

    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        Workbook wb=new HSSFWorkbook(file.getInputStream());
        Sheet sheet=wb.getSheetAt(0);
        int lastRowNum=sheet.getLastRowNum();
      for(int i=1;i<=lastRowNum;i++){
          Row row = sheet.getRow(i);
          Employee employee = new Employee();
          employee.setUsername(row.getCell(0).getStringCellValue());
          employee.setName(row.getCell(1).getStringCellValue());
          Double numericCellValue = row.getCell(2).getNumericCellValue();
          employee.setAge(numericCellValue.intValue());
          employee.setEmail(row.getCell(3).getStringCellValue());
          employee.setAdmin(false);
          employee.setPassword("123");
          employeeMapper.insert(employee);
      }
    }
}
